<template>
  <div>
    <el-form :inline="true" class="user-search">
      <el-form-item>
        <el-input size="small" v-model="queryForm.username" clearable placeholder="用户名"></el-input>
      </el-form-item>
      <el-button size="small" type="primary" icon="el-icon-search" @click="list">查询</el-button>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="doAdd">增加</el-button>
    </el-form>

    <el-table :data="tableData" border style="width: 100%" height="510" :header-cell-style="{textAlign: 'center'}">
      <!-- <el-table-column fixed prop="userId" label="ID" width="50">
      </el-table-column> -->
      <el-table-column label="用户名" prop="username">
      </el-table-column>
      <el-table-column label="邮箱" prop="email">
      </el-table-column>
      <el-table-column label="手机号" prop="mobile">
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template scope="scope">
          <p v-if="scope.row.status==0">正常</p>
          <p v-if="scope.row.status==1">禁用</p>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="doDel(scope.$index, scope.row)" type="primary" size="small" icon="el-icon-delete">删除</el-button>
          <el-button @click="doEdit(scope.$index, scope.row)" type="primary" size="small" icon="el-icon-edit">修改</el-button>
          <el-input type="hidden" v-model="mergeForm.userId"></el-input>
        </template>
      </el-table-column>

    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryForm.page"
        :page-sizes="[5,10, 25, 50, 100]" :page-size="queryForm.rows" layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        queryForm: {
          username: null,
          page: 1,
          rows: 10,
          total: 0
        },
        mergeForm: {
          userId: null
        },
        tableData: []
      }
    },
    methods: {
      list: function() {
        // console.log(1111);
        this.axios.post(this.axios.urls.SYS_USER_LIST, this.queryForm).then((response) => {
          this.tableData = response.data.result
          this.queryForm.page = response.data.page
          this.queryForm.rows = response.data.rows
          this.queryForm.total = response.data.total
        })
      },
      doAdd: function() {
        this.$router.push('/AppMain/userAdd')
      },
      doEdit: function(index, row) {
        this.$store.commit('setUser', {
            user: row,
        })
        this.$router.push('/AppMain/userEdit');
      },
      doDel: function(index, row) {
        this.$confirm('您确定要删除该条记录?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post(this.axios.urls.SYS_USER_DEL, {
            userId: row.userId
          }).then((response) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.list()
          })
        })
      },
      handleSizeChange(rows) {
        // console.log('handleSizeChange');
        this.queryForm.page = 1
        this.queryForm.rows = rows
        this.list()
      },
      handleCurrentChange(page) {
        // console.log('handleCurrentChange');
        this.queryForm.page = page
        this.list()
      }
    },
    created() {
      this.list()
    }
  }
</script>

<style>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }

  .block {
    margin-top: 20px;
    text-align: right;
  }
</style>
